lighthouse: tree-shake bundled libs #6563
Conversation
Replace the blanket `import * as` of every bundled library with per-library named imports collected from the app's actual static usage (plus tags captured during dynamic-component serialization), so Rolldown can drop unused exports. Keep star imports for internal `$/utils/*` modules, `react`/`@emotion/react`, and any tag set that isn't a valid JS identifier. Plumb `theme_roots` through the Radix Themes plugin and the Tailwind v3/v4 root style so only the Radix CSS token files for accent/gray colors referenced by `Theme` components are emitted, with the monolithic stylesheet retained as a fallback for state-driven or unrecognized colors.
Merging this PR will not alter performance
Comparing Footnotes
|
Greptile SummaryThis PR replaces blanket
Confidence Score: 4/5The change is safe to merge with one edge case to keep in mind: an external bundled library that is never imported as a named export will be silently absent from window.__reflex, which could cause a runtime failure if a dynamically-eval'd component relies on it. The core logic is correct and well-tested. The dynamic_component_imports mechanism correctly captures imports from serialized dynamic components before collect_window_library_imports runs. The react/@emotion/react always-star-import exemption preserves the window.__reflex.react access pattern expected by state.js. The main risk is the missing-library scenario where an external bundled library with no static named imports quietly disappears from window.__reflex — the if tags: guard silently drops it rather than falling back to a star import. reflex/compiler/compiler.py — the collect_window_library_imports logic and its interaction with dynamic_component_imports; packages/reflex-base/src/reflex_base/compiler/templates.py — the _render_window_reflex_block output. Important Files Changed
Reviews (3): Last reviewed commit: "refactor(reflex-base): clarify import-na..." | Re-trigger Greptile |
When accent_color and its paired gray resolve to the same scale (e.g. accent_color="gray"), the tokens CSS file was imported twice. Subtract accents from grays before extending the sheet list so each color file is emitted only once.
|
Add more tests for dynamic components. |
Drop the per-color-scale tree-shaking that emitted granular @radix-ui/themes/tokens/colors/<color>.css imports and threaded theme_roots through the compiler and plugin context. Restore the single @radix-ui/themes/styles.css import, removing get_radix_themes_stylesheets, strip_radix_theme_imports, and the PreCompileContext.theme_roots field.
| """ | ||
| from reflex.compiler.compiler import RADIX_THEMES_STYLESHEET | ||
| from reflex_components_radix.plugin import RADIX_THEMES_STYLESHEET | ||
|
|
There was a problem hiding this comment.
can we figure a way such that tailwind doesn't conceptually depend on radix themes?
| component_imports = component._get_all_imports() | ||
| compiler._apply_common_imports(component_imports) | ||
|
|
||
| for lib, ivs in component_imports.items(): |
There was a problem hiding this comment.
please avoid uncommon abbreviations like ivs in variable names
| entries: list[str] = [] | ||
| for lib, names in window_library_imports.items(): | ||
| alias = f"__reflex_{_normalize_window_lib_alias(lib)}" | ||
| if names is None or any(not _JS_IDENTIFIER_RE.match(n) for n in names): |
There was a problem hiding this comment.
what does any(not _JS_IDENTIFIER_RE.match(n) for n in names) mean? can we create a private helper function with a name for it?
# Conflicts: # reflex/compiler/compiler.py
Extract _all_valid_js_identifiers helper for the window-reflex import check, and rename cryptic loop variables (ivs/iv -> import_vars/ import_var) in the dynamic component serializer. No behavior change.
Replace the blanket
import * asof every bundled library with per-library named imports collected from the app's actual static usage (plus tags captured during dynamic-component serialization), so Rolldown can drop unused exports. Keep star imports for internal$/utils/*modules,react/@emotion/react, and any tag set that isn't a valid JS identifier.Plumb
theme_rootsthrough the Radix Themes plugin and the Tailwind v3/v4 root style so only the Radix CSS token files for accent/gray colors referenced byThemecomponents are emitted, with the monolithic stylesheet retained as a fallback for state-driven or unrecognized colors.All Submissions:
Type of change
Please delete options that are not relevant.
New Feature Submission:
Changes To Core Features:
fixes ENG-9323
